package org.jeecg.modules.lab.syn;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.TokenServess;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.entrust.entity.LabEntrustItems;
import org.jeecg.modules.lab.entrust.service.ILabEntrustItemsService;
import org.jeecg.modules.lab.entrust.service.ILabWentrustService;
import org.jeecg.modules.lab.util.ReportLoad;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;

@Slf4j
@Component
public class SignatureJob {
    @Autowired
    private ILabEntrustItemsService labEntrustItemsService;
    @Autowired
    private ILabWentrustService labWentrustService;
    @Autowired
    private ILabCronJobService labCronJobService;
    /**
     * 注入线程池
     */
    @Resource(name = "executorService")
    private ExecutorService threadExecutor;
    @XxlJob(value = "SignatureJob")
    public ReturnT<String> signatureJob(String params) {
        TokenServess tokenServess = new TokenServess();
        QueryWrapper<LabEntrustItems> labEntrustItemsQueryWrapper = new QueryWrapper<>();
        labEntrustItemsQueryWrapper.eq("signature_status","待同步签名")
                .isNotNull("codes").ne("codes","");
        List<LabEntrustItems> list = labEntrustItemsService.list(labEntrustItemsQueryWrapper);
        //执行任务逻辑
        for (LabEntrustItems labEntrustItems : list){
            Callable<Boolean> c = () -> {
                try {
                    String tokenServe = tokenServess.getTokenServe();
                    LoginUser loginUser = labWentrustService.getOneUser(labEntrustItems.getUserId());
                    String employeeTicket = tokenServess.getEmployeeTicket(loginUser.getTId(), "", tokenServe);
                    //从数据库查询随机码
                    String s = tokenServess.mobileSignature(employeeTicket, labEntrustItems.getCodes(), labEntrustItems.toString());

                    ReportLoad reportLoad = new ReportLoad();
                    String ssa = reportLoad.upSignature(loginUser.getRealname(), s);
                    labWentrustService.updatasz(loginUser.getTId(), ssa);
                    labEntrustItems.setSynFlag("1");
                    labEntrustItems.setItemStatus("待复核");
                    labEntrustItems.setSignatureStatus("已同步签名");
                    labEntrustItemsService.updateById(labEntrustItems);
                    if (MyEnum.sync) {
                        //同步表信息
                        long time = new Date().getTime();
                        ArrayList<LabCronJob> jobs = new ArrayList<>();
                        jobs.add(new LabCronJob("lab_entrust_items", "1", time, 1));
                        labCronJobService.saveBatch(jobs);
                    }
                } catch (Exception e) {
                    labEntrustItems.setSynFlag("1");
                    labEntrustItems.setSignatureStatus("同步签名失败");
                    labEntrustItemsService.updateById(labEntrustItems);
                    if (MyEnum.sync) {
                        //同步表信息
                        long time = new Date().getTime();
                        ArrayList<LabCronJob> jobs = new ArrayList<>();
                        jobs.add(new LabCronJob("lab_entrust_items", "1", time, 1));
                        labCronJobService.saveBatch(jobs);
                    }
                }
                return true;
            };
            threadExecutor.submit(c);
        }
        return ReturnT.SUCCESS;
    }
}
